package com.cvte.boe.media;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.cvte.boe.media.player.AudioPlayer;
import com.cvte.boe.media.utils.OrientationUtil;
import com.cvte.boe.media.utils.StringUtils;
import com.cvte.boe.media.views.widget.LongClickImageButton;
import com.cvte.boe.media.views.widget.LrcView;
import com.cvte.boe.media.views.widget.MarqueeTextView;
import com.cvte.boe.media.views.widget.ReflectionImage;

import java.io.File;

/**
 * Created by wuguitong on 2018/4/18.
 */
public class AudioPlayActivity extends Activity implements View.OnClickListener,AudioPlayer.OnAudioPlayerListener{
    private String mCurrentPath;
    private Uri intentUri;
    private Context mContext;
    private LongClickImageButton ibtnPauseStart;
    private LongClickImageButton ibtnListMenu;
    private LongClickImageButton ibtnPlayMode;
    private LongClickImageButton ibtnSwitchToPre;
    private LongClickImageButton ibtnSwitchToNext;
    private LongClickImageButton ibtnBackward;
    private LongClickImageButton ibtnForward;
    private SeekBar sbMusicTimeBar;
    private MarqueeTextView mtvFileName;
    private TextView tvCurTime;
    private TextView tvDurTime;
    private ReflectionImage albumReflectionImage;
    private AudioPlayer mAudioPlayer;
    private LrcView mLrcView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        if(OrientationUtil.isLandscapeOrientation(mContext)){
            setContentView(R.layout.activity_music_player_land);
        }else{
            setContentView(R.layout.activity_music_player_port);
        }
        Intent intent = getIntent();
        String action = intent.getAction();
        if (Intent.ACTION_VIEW.equals(action)) {
            intentUri = intent.getData();
            if(intentUri != null){
                mCurrentPath = new File(intentUri.getPath()).getParent();
            }
        }
        MediaApplication.registerMountActionListener(mountActionListener);
        ibtnPauseStart = (LongClickImageButton)findViewById(R.id.ibtn_music_pause_start);
        ibtnPauseStart.setOnClickListener(this);
        ibtnSwitchToPre = (LongClickImageButton)findViewById(R.id.ibtn_music_pre);
        ibtnSwitchToPre.setOnClickListener(this);
        ibtnSwitchToNext = (LongClickImageButton)findViewById(R.id.ibtn_music_next);
        ibtnSwitchToNext.setOnClickListener(this);
        ibtnBackward = (LongClickImageButton)findViewById(R.id.ibtn_music_backward);
        ibtnBackward.setOnClickListener(this);
        ibtnForward  = (LongClickImageButton)findViewById(R.id.ibtn_music_forward);
        ibtnForward.setOnClickListener(this);
        ibtnPlayMode = (LongClickImageButton)findViewById(R.id.ibtn_music_play_mode);
        ibtnPlayMode.setOnClickListener(this);
        ibtnListMenu = (LongClickImageButton)findViewById(R.id.ibtn_music_list_menu);
        ibtnListMenu.setOnClickListener(this);
        mtvFileName = (MarqueeTextView)findViewById(R.id.mtv_music_file_name);
        mtvFileName.setMarqueeState(true);
        albumReflectionImage = (ReflectionImage)findViewById(R.id.ri_music_album_art);
        sbMusicTimeBar = (SeekBar)findViewById(R.id.sb_music_time);
        tvCurTime = (TextView)findViewById(R.id.tv_music_cur_time);
        tvDurTime = (TextView)findViewById(R.id.tv_music_dur_time);
        mLrcView = (LrcView)findViewById(R.id.lrc_view);
        mAudioPlayer = new AudioPlayer();
        mAudioPlayer.setOnAudioPlayerListener(this);
        mAudioPlayer.initAudioPlayer(mContext,intentUri.getPath(),mCurrentPath,albumReflectionImage,
                ibtnPauseStart,ibtnPlayMode,sbMusicTimeBar,tvCurTime,tvDurTime,mtvFileName,mLrcView);
    }

    @Override
    protected void onDestroy() {
        MediaApplication.unRegisterMountActionListener(mountActionListener);
        mAudioPlayer.deinitAudioPlayer();
        super.onDestroy();
    }
    MediaApplication.MountActionListener mountActionListener = new MediaApplication.MountActionListener() {
        @Override
        public void onReceive(String volumePath, boolean mount) {
            if (StringUtils.formatMountPointWithSeparator(mCurrentPath).startsWith(StringUtils.formatMountPointWithSeparator(volumePath))) {
                if (!mount) {
                    AudioPlayActivity.this.finish();
                }
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                this.finish();
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ibtn_music_pause_start:
                mAudioPlayer.changeAudioPlayStatus();
                break;
            case R.id.ibtn_music_backward:
                mAudioPlayer.backwardAudio();
                break;
            case R.id.ibtn_music_forward:
                mAudioPlayer.forwardAudio();
                break;
            case R.id.ibtn_music_next:
                mAudioPlayer.nextAudio();
                break;
            case R.id.ibtn_music_pre:
                mAudioPlayer.preAudio();
                break;
            case R.id.ibtn_music_play_mode:
                mAudioPlayer.changeAudioPlayMode();
                break;
            case R.id.ibtn_music_list_menu:
                mAudioPlayer.showAudioFileListSelect();
                break;
            default:
                break;
        }
    }

    @Override
    public void OnAudioDestroy() {
        this.finish();
    }

    @Override
    public void OnAudioUnsupport() {
        this.finish();
    }
}
